import os
import sys
import numpy as np
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from OCC.Display.backend import load_backend
load_backend("qt-pyqt5")
import OCC.Display.qtDisplay as qtDisplay
import model
import po

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.model = model.TraitsClass()
        self.po = po.TraitsClass()
        self.po.model=self.model
        self.resize(800, 600)
        menubar = QMenuBar()
        self.setMenuBar(menubar)
        self.statusbar = QStatusBar(self)
        self.setStatusBar(self.statusbar)
##############################################################################
        menu = QMenu('模型', menubar)
        menubar.addMenu(menu)

        def actionFunction():
            self.model.generateBox()
            self.displayShape()
        action = QAction('立方块', self)
        menu.addAction(action)
        action.triggered.connect(actionFunction)

        def actionFunction():
            if fileName := QFileDialog.getOpenFileName(filter='STEP(*.stp;*.step)')[0]:
                self.model.importStep(fileName)
                self.displayShape()
        action = QAction('导入stp', self)
        menu.addAction(action)
        action.triggered.connect(actionFunction)

        def actionFunction():
            if fileName := QFileDialog.getOpenFileName(filter='IGES(*.igs;*.iges)')[0]:
                self.model.importIges(fileName)
                self.displayShape()
        action = QAction('导入igs', self)
        menu.addAction(action)
        action.triggered.connect(actionFunction)

        def actionFunction():
            if fileName := QFileDialog.getSaveFileName(filter='STEP(*.stp;*.step)')[0]:
                self.model.exportStep(fileName)
                self.displayShape()
        action = QAction('导出stp', self)
        menu.addAction(action)
        action.triggered.connect(actionFunction)

        def actionFunction():
            if fileName := QFileDialog.getSaveFileName(filter='IGES(*.igs;*.iges)')[0]:
                self.model.exportIges(fileName)
                self.displayShape()
        action = QAction('导出igs', self)
        menu.addAction(action)
        action.triggered.connect(actionFunction)

        def actionFunction():
            x,y,z=self.model.shapeSize()
            QMessageBox.information(self, '尺寸', f'x方向：{x}\ny方向：{y}\nz方向：{z}')
        action = QAction('尺寸', self)
        menu.addAction(action)
        action.triggered.connect(actionFunction)
        ##############################################################################
        menu = QMenu('网格', menubar)
        menubar.addMenu(menu)
        def actionFunction():
            self.model.shapeToMesh()
            self.displayMesh()
        action = QAction('网格剖分', self)
        menu.addAction(action)
        action.triggered.connect(actionFunction)

        def actionFunction():
            if fileName := QFileDialog.getOpenFileName(filter='STL(*.stl)')[0]:
                self.model.importMesh(fileName)
                self.displayMesh()
        action = QAction('导入stl', self)
        menu.addAction(action)
        action.triggered.connect(actionFunction)

        def actionFunction():
            if fileName := QFileDialog.getOpenFileName(filter='PLY(*.ply)')[0]:
                self.model.importMesh(fileName)
                self.displayMesh()
        action = QAction('导入ply', self)
        menu.addAction(action)
        action.triggered.connect(actionFunction)

        def actionFunction():
            x,y,z=self.model.meshSize()
            QMessageBox.information(self, '尺寸', f'x轴：{x}\ny轴：{y}\nz轴：{z}')
        action = QAction('尺寸', self)
        menu.addAction(action)
        action.triggered.connect(actionFunction)

        ##############################################################################
        menu = QMenu('PO法', menubar)
        menubar.addMenu(menu)

        def actionFunction():
            self.po.configure_traits(view='parameter_view')
        action = QAction('参数设置', self)
        menu.addAction(action)
        action.triggered.connect(actionFunction)

        def actionFunction():
            self.po.thread.start()
            self.centralWidget().setParent(None)
            self.setCentralWidget(self.po.edit_traits().control)
        action = QAction('计算', self)
        menu.addAction(action)
        action.triggered.connect(actionFunction)

        def actionFunction():
            self.po.thread.terminate()
            self.po.thread.wait()
        action = QAction('停止', self)
        menu.addAction(action)
        action.triggered.connect(actionFunction)

        ##############################################################################
        menu = QMenu('窗口', menubar)
        menubar.addMenu(menu)

        def actionFunction():
            self.centralWidget().setParent(None)
            self.setCentralWidget(self.centralwidget)
        action = QAction('模型', self)
        menu.addAction(action)
        action.triggered.connect(actionFunction)

        def actionFunction():
            self.centralWidget().setParent(None)
            self.setCentralWidget(self.centralwidget1)
        action = QAction('网格', self)
        menu.addAction(action)
        action.triggered.connect(actionFunction)

        def actionFunction():
            self.centralWidget().setParent(None)
            self.setCentralWidget(self.po.edit_traits().control)
        action = QAction('PO法', self)
        menu.addAction(action)
        action.triggered.connect(actionFunction)
        ##############################################################################

        self.centralwidget = QWidget()
        horizontalLayout = QHBoxLayout(self.centralwidget)

        self.canvas = qtDisplay.qtViewer3d(self)
        self.canvas.InitDriver()  # canva的驱动,设置驱动后，才能成功display
        self.display = self.canvas._display
        self.display.set_bg_gradient_color([0, 128, 0], [128, 128, 128])  # 设置背景渐变色
        self.display.display_triedron()  # display black trihedron

        horizontalLayout.addWidget(self.canvas)

        self.centralwidget1 = QWidget()
        horizontalLayout = QHBoxLayout(self.centralwidget1)
        pushButton = QPushButton('haha1')
        horizontalLayout.addWidget(pushButton)
        pushButton.clicked.connect(lambda: (
            print('fjdksf'),
        ))
        pushButton = QPushButton('haha2')
        horizontalLayout.addWidget(pushButton)
        pushButton.clicked.connect(lambda: (
            print('fjdksf12'),
        ))

        pushButton = QPushButton('haha3')
        horizontalLayout.addWidget(pushButton)
        pushButton.clicked.connect(lambda: (
            print('fjdksf13'),
        ))







        self.setCentralWidget(self.centralwidget)

        self.displayShape()

    def displayShape(self):
        if self.model.shape is None:
            return
        self.display.EraseAll()
        self.display.DisplayShape(self.model.shape, update=True)
        self.display.FitAll()

    def displayMesh(self):
        if self.model.mesh is None:
            return
        self.display.EraseAll()
        self.display.DisplayShape(self.model.mesh, update=True)
        self.display.FitAll()


if __name__ == "__main__":

    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())
